<?php

namespace Addons\IcWaterGoods\Controller;
use Mp\Controller\AddonsController;

/**
 * 水产圈后台管理控制器
 * @author 16，陈浩南
 */
class WebController extends AddonsController {
  /**/
  public function lists(){
        $model_name = I('get.model');
        $model = get_addon_model($model_name);
        $this->setModel($model);
        if($model_name == 'good_lists'){
            $typeModels = D('Addons://IcWaterGoods/IcGoodsTypes');
            $types = $typeModels->getTypesList();
            $this->addListItem('good_type', '信息分类', 'enum', array('options'=>$types));
            $this->addListItem('id', '操作', 'custom', array('options'=>array(
                'edit'=>array('title' => '查看','url'=>web_urls('custEdit',array('id'=>'{id}','model'=>'good_oper')),'class'=>'btn btn-success btn-sm icon-edit'),
                'del'=>array('title' => '删除','url'=>create_addon_url('delete',array('id'=>'{id}','model'=>'good_oper','db'=>'ic_goods')),'class'=>'btn btn-success btn-sm icon-delete'),
                'lists'=>array('title' => '评论','url'=>create_addon_url('lists',array('pid'=>'{id}','model'=>'pin_lists')),'class'=>'btn btn-success btn-sm icon-delete'),
                ),));
        }elseif($model_name == 'usertype_lists'){
            $this->addListItem('fid', '权限列表', 'function', array('function_name' => 'get_Lists'));
            $this->addListItem('id', '操作', 'custom', array('options'=>array(
                'edit'=>array('title' => '编辑','url'=>web_urls('FollowEdit',array('id'=>'{id}','model'=>'usertype_oper')),'class'=>'btn btn-success btn-sm icon-edit'),
            ),));
        }
        $this->common_lists(); 
  }

  /*查看信息*/
  public function custEdit(){
      $model_name = I('get.model');
      $model = get_addon_model($model_name);
      $ls = explode('_', $model_name);
      $lists = $ls[0].'_lists';
      $url = create_addon_url('lists',array('model'=>$lists,'pid'=>I('get.pid')));  //设置跳转成功url
      if(IS_POST){
          $this->success('',$url);
      }else{
          /*获取图片信息*/
          $imgs = json_decode(M('IcGoods')->where(array('id'=>I('id')))->getField('imgs'));
          $typeModels = D('Addons://IcWaterGoods/IcGoodsTypes');
          $types = $typeModels->getTypesList();
          $this->setModel($model)
               ->setEditMap(array('id'=>I('get.id')))
               ->addFormField('shop_name', '商店名', 'text')
              ->addFormField('describe', '描述', 'textarea')
               ->addFormField('good_type', '货品分类', 'select',array('options'=>$types))
               ->addFormField('create_time','发布时间', 'time');
               foreach($imgs as $key => $val){
                   $this->addFormField('imgs'.$key, '图片', 'image',array('value'=>$val->url));
               }
          $this->common_edit();
      }
  }

  /*添加*/
  public function add(){
	    $model_name = I('get.model');
	    $model = get_addon_model($model_name);
	    $ls = explode('_', $model_name);
      $lists = $ls[0].'_lists';
	    $url = create_addon_url('lists',array('model'=>$lists,'pid'=>I('get.pid')));	//设置跳转成功url
      file_put_contents('text.txt',$url);
	    $this->setModel($model)->addAuto('create_time',time())->setAddSuccessUrl($url);
      /*子分类添加*/
      if(I('pid',0,'intval'))  $this->addAuto('pid',I('get.pid'));
	    $this->common_add();
  }

  /*编辑*/
  public function edit(){
    $model_name = I('get.model');
    $model = get_addon_model($model_name);
    $ls = explode('_', $model_name);
    $lists = $ls[0].'_lists';
    $url = create_addon_url('lists',array('model'=>$lists,'pid'=>I('get.pid')));  //设置跳转成功url

    $this->setModel($model)->addAuto('update_time',time())->setEditMap(array('id'=>I('get.id')))->setEditSuccessUrl($url) ;
      if($model_name == 'usertype_oper'){
          $typeModels = D('Addons://IcWaterGoods/IcGoodsTypes');
          $types = $typeModels->getTypesList();
          $this ->addFormField('fid', '发布权限', 'checkbox',array('options'=>$types));
    }
    $this->common_edit();
  }

  /*设置用户权限*/
  public function FollowEdit(){
      $model_name = I('get.model');
      $model = get_addon_model($model_name);
      $ls = explode('_', $model_name);
      $lists = $ls[0].'_lists';
      $url = create_addon_url('lists',array('model'=>$lists,'pid'=>I('get.pid')));  //设置跳转成功url
      if(IS_POST){
          $fid = I('post.fid');
          $fid = json_encode($fid);
          if(M('IcFollow')->where(array('id'=>I('get.id')))->setField('fid',$fid)){
              $this->success('成功',$url);
          }else{
              $this->error('错误');
          }
      }else{


          $this->setModel($model)->addAuto('update_time',time())->setEditMap(array('id'=>I('get.id')))->setEditSuccessUrl($url) ;
          if($model_name == 'usertype_oper'){
              $typeModels = D('Addons://IcWaterGoods/IcGoodsTypes');
              $types = $typeModels->getTypesList();
              $this ->addFormField('fid', '发布权限', 'checkbox',array('options'=>$types));
          }
          $this->common_edit();
      }

  }

  /*数据删除*/
    public function delete(){
       $model_name = I('get.model');
       $ls = explode('_', $model_name);
      $lists = $ls[0].'_lists';
       $url = create_addon_url('lists',array('model'=>$lists,'pid'=>I('get.pid'))); //设置跳转成功url
       if(M(I('get.db'))->where('id = '.I('get.id'))->setField('status',0)){
       		$this->success('删除成功',$url);
       }else{
       		$this->error('删除失败');
       }
    }

    /*真删除*/
     public function true_delete(){
         $model_name = I('get.model');
         $ls = explode('_', $model_name);
         $lists = $ls[0].'_lists';
         $url = create_addon_url('lists',array('model'=>$lists,'pid'=>I('get.pid')));  //设置跳转成功url
         if(M(I('get.db'))->where('id = '.I('get.id'))->delete()){
            $this->success('删除成功',$url);
         }else{
            $this->error('删除失败');
         }
      }

    /*分类删除*/
    public function types_delete(){
    	/*真删除*/
       $model_name = I('get.model');
       $ls = explode('_', $model_name);
      $lists = $ls[0].'_lists';
      $url = create_addon_url('lists',array('model'=>$lists,'pid'=>I('get.pid')));  //设置跳转成功url
       $model      = M(I('get.db'));
       $id 		   = I('get.id');
       if($model->where(array('pid'=>$id))->select()){
       	   if($model->where(array('pid'=>$id))->delete()){
       	  	 $model->where(array('id'=>$id))->delete();
       	  }
       }else{
       		$model->where(array('id'=>$id))->delete();
       }
       $this->success('删除成功!',$url);
    }

    /*认证审核*/
    public function setStatus(){
   	   $model_name = I('get.model');
       $ls = explode('_', $model_name);
      $lists = $ls[0].'_lists';
       $url = create_addon_url('lists',array('model'=>$lists,'pid'=>I('get.pid')));  //设置跳转成功url
       if(M('ic_authentication')->where('id = '.I('get.id'))->setField('status',2)){
       		$this->success('审核通过',$url);
       }else{
       		$this->error('审核失败');
       }
   }
}

?>